package com.wgj.android.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.jiaai.xui.adapter.XRecyclerViewAdapter;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.adapter.BankAdapter;
import com.wgj.android.entity.BankInfo;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/9.
 */

public class BanckCardDialog extends AppCompatDialogFragment {
    private View view;
    private  OnSelectBankCallback bankCallback;
    private Dialog dialog;


    @BindView(R.id.loadingView)
    XLoadingView loadingView;

    @BindView(R.id.backListView)
    RecyclerView backListView;

    BankAdapter bankAdapter;
    private List<BankInfo> bankInfos=new ArrayList<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null){
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.bank_card_dialog, null, false);
        }


        ButterKnife.bind(this,view);
        bankAdapter=new BankAdapter(backListView,bankInfos);
        bankAdapter.setOnItemClickListener(new XRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                if(bankCallback!=null){
                    bankCallback.onSelect(bankInfos.get(i));
                }
                dismiss();

            }
        });
        backListView.setHasFixedSize(true);
        backListView.setLayoutManager(new LinearLayoutManager(getContext()));
        backListView.setAdapter(bankAdapter);
        loadData();
    }
    public  void  loadData(){
        XHttp.obtain().get(WGJAPI.FIND_BANK_URL+"?mId="+CacheUtils.getMember().getM_id(), null, new ListHttpCallBack<BankInfo>() {

            @Override
            public void onSuccess(List<BankInfo> list) {
                 if(list==null ||list.size()==0){
                    loadingView.showEmpty("亲,你还没有绑定银行卡哦");
                }else {
                    bankInfos.clear();
                    bankInfos.addAll(list);
                    bankAdapter.notifyDataSetChanged();
                    loadingView.showContent();
                }


            }
            @Override
            public void onFailed(int i, String s, boolean b) {

                if(b){
                    loadingView.showNoNetwork();
                }else {
                    loadingView.showError();
                }
            }
        });

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);
        if(dialog==null){
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.popupwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width =WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }

    public BanckCardDialog setBankCallback(OnSelectBankCallback bankCallback) {
        this.bankCallback = bankCallback;
        return this;
    }



    @OnClick(R.id.btExit)
    public  void onExit(View view){
        dismiss();
    }
    public  interface  OnSelectBankCallback{
        public  void  onSelect(BankInfo bankInfo);
    }
}
